package com.example.administrator.testc.FragmentTagHostViewpager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.administrator.testc.FragmentTabHost.FragmentPage;
import com.example.administrator.testc.PagerAdapter.FragmentAdapter;
import com.example.administrator.testc.PagerAdapter.TestFragment;
import com.example.administrator.testc.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangyu
 *         功能描述：自定义TabHost
 */
public class TestMain extends AppCompatActivity {

    // 定义FragmentTabHost对象
    private FragmentTabHost mTabHost;
    // 定义一个布局
    private LayoutInflater layoutInflater;
    // 定义数组来存放Fragment界面
    private Class fragmentArray[] = { FragmentPage.class, FragmentPage.class,
            FragmentPage.class, FragmentPage.class, FragmentPage.class };
    // 定义数组来存放按钮图片
    private int mImageViewArray[] = { R.drawable.tab_home_btn,
            R.drawable.home_sel_icon, R.drawable.me_sel_icon,
            R.drawable.share_sel_icon, R.drawable.home_sel_icon };
    // Tab选项卡的文字
    private String mTextviewArray[] = { "首页", "消息", "好友", "广场", "更多" };
    private ViewPager vp;
    private List<Fragment> mDates = new ArrayList<Fragment>();
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tabviewpager_layout);
        vp = (ViewPager) findViewById(R.id.realtabcontent);
        initPager();
        initView();
    }

    /**
     * 初始化组件
     */
    private void initView() {
        vp.setOnPageChangeListener(new ViewPagerListener());
        // MyAdapter adapter = new MyAdapter();
        // vp.setAdapter(adapter);
        // 实例化布局对象
        layoutInflater = LayoutInflater.from(this);
        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        // 得到对象后，初始化
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.setOnTabChangedListener(new TabHostListener());
        // 得到fragment的个数
        int count = fragmentArray.length;

        for (int i = 0; i < count; i++) {
            // 为每一个Tab按钮设置图标、文字和内容
            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.setTag(i);
            // 设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.selector_tab_background);
        }
    }
    private void initPager() {
        mDates.add(new TestFragment(Color.RED));
        mDates.add(new TestFragment(Color.GREEN));
        mDates.add(new TestFragment(Color.BLUE));
        mDates.add(new TestFragment(Color.YELLOW));
        mDates.add(new TestFragment(Color.WHITE));
        vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }
    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);
        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(mTextviewArray[index]);
        return view;
    }

    private class TabHostListener implements TabHost.OnTabChangeListener {
        @Override
        public void onTabChanged(String tabId) {
            int position = mTabHost.getCurrentTab();
            vp.setCurrentItem(position);
        }
    }
    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            return mDates.get(arg0);
        }

        @Override
        public int getCount() {
            return mDates.size();
        }

    }

    class ViewPagerListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int index) {
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(index);
            widget.setDescendantFocusability(oldFocusability);
        }
    }
}